<?php
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	#CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id
	
	+ Module  		: t_senddata Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_t_senddata.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 11:40:14
	
*/

//class of t_senddata
class C_t_senddata extends CI_Controller {

	//constructor
	function __construct(){
		parent::__construct();
		
		$this->load->model('m_t_senddata', '', TRUE);
		checkLogin();
	}
	
	//set index
	function index(){
		$this->load->view('main/v_t_senddata');
	}
	
	function adddet_list(){
		$kontrak_no = isset($_POST['kontrak_no']) ? @$_POST['kontrak_no'] : "";
		$add_id = isset($_POST['add_id']) ? @$_POST['add_id'] : "";
		$result=$this->m_t_senddata->adddet_list($kontrak_no,$add_id);
		echo $result;
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->t_senddata_list();
				break;
			case "SENDDATA":
				$this->t_senddata_senddata();
			break;
			case "getdata_doctrac_mproc":
				$this->getdata_doctrac_mproc();
			break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function t_senddata_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_t_senddata->t_senddata_list($query,$start,$end);
		echo $result;
	}
	function getdata_doctrac_mproc(){
		$result=$this->m_t_senddata->getdata_doctrac_mproc();
		echo $result;
	}
	function get_dataadendum(){
		$add_id = trim(@$_POST['add_id']);
		$result=$this->m_t_senddata->get_dataadendum($add_id);
		echo $result;
	}
	
	//function for create new record
	function t_senddata_senddata(){
		$kontrak_id = trim(@$_POST['kontrak_id']);
		$jenis_data = trim(@$_POST['jenis_data']);
		$document_id = "";
		$data = $this->m_t_senddata->get_dataforsend($kontrak_id, $jenis_data);
		foreach($data as $row_data){
			$kontrak_id_induk = $row_data->kontrak_id;
			$branch_id = $row_data->branch_id;
			$project_kode = $row_data->project_code;
			$project_name = $row_data->project_name;
			$job_name = $row_data->job_name;
			$project_value = $row_data->project_value;
			$vendor_name = $row_data->vendor_name;
			$kontrak_no = $row_data->kontrak_no;
			$kontrak_date = $row_data->kontrak_date;
			$kontrak_startdate = $row_data->kontrak_begin;
			$kontrak_enddate = $row_data->kontrak_end;
			$nego_bayar = $row_data->nego_bayar;
			$nego_rapktotal = $row_data->nego_rapktotal;
			$m_div_id = $row_data->m_div_id;
			$rapk_procurement = $row_data->rapk_procurement;
		}
		if($jenis_data == 'Adendum'){
			$document_id = 3;
		}else{
			if($m_div_id == 1){
				$document_id = 1;
			}else if($m_div_id == 2){
				$document_id = 2;
			}
		}
		
		// cek apakah di database doctrac ada proyek atau tidak, jika tidak maka tambah baru
		$doctrac_projectlist_id = $this->m_t_senddata->doctrac_checkproject($project_kode);
		if($doctrac_projectlist_id == 0){
			$projectlist_id = $this->m_t_senddata->create_doctracprojectlist($project_kode, $project_name,$branch_id);
		}else{
			$projectlist_id = $doctrac_projectlist_id;
        }
		// cek apakah di database doctrac ada company atau tidak, jika tidak maka tambah baru
		$doctrac_company_id = $this->m_t_senddata->doctrac_checkcompany($vendor_name);
		if($doctrac_company_id == 0){
			$company_id = $this->m_t_senddata->create_doctraccompany($vendor_name);
		}else{
			$company_id = $doctrac_company_id;
        }
		
		
		if($document_id == 5 || $document_id == 6){
			$procru = 0;
		}else{
			$procru = $rapk_procurement;
		}
		$data_umum = array(
			'm_document_document_id' => $document_id,					       	
			'projdoc_status'=>'y',
			't_projectlist_projectlist_id' =>$projectlist_id,
			'm_proc_proc_id' =>$procru,
			'm_company_company_id' => $company_id,
			'projdoc_value' => $project_value,
			'projdoc_occupations' => $job_name,
			'projdoc_finished'=>'n',
		);
		if ($document_id == 1 || $document_id == 2 || $document_id == 3){
			$data_khusus = array(
				'projdoc_contract_no'=>$kontrak_no,
				'projdoc_contract_date'=>date('Y-m-d h:i:s', strtotime($kontrak_date)),
				'procdoc_end_date'=>date('Y-m-d h:i:s', strtotime($kontrak_enddate)),
				'procdoc_start_date'=>date('Y-m-d h:i:s', strtotime($kontrak_startdate)),
				'projdoc_payment'=>$nego_bayar,
				'projdoc_value_rapk'=>$nego_rapktotal
			);
		}else if ($document_id == 4 || $document_id == 5 || $document_id == 6){
			$data_khusus =	array(
				'projdoc_bap_value'=>$_POST['projdoc_bap_value'],
				'projdoc_tagihan_ke'=>$_POST['projdoc_tagihan_ke']
			);
		}
		$data['table'] = array_merge($data_umum,$data_khusus);
		if($document_id != 1 && $document_id != 2){
				 $data['table'] = array_merge( $data['table'],array('projdoc_parent_id'=>$kontrak_id_induk));
		}else{
				 $data['table'] = array_merge( $data['table'],array('projdoc_parent_id'=>0));
		}
		if($document_id == 4 ){
			if($_POST['projdoc_last_addendum'] != '-'){
				$projdoc_contract_no = $_POST['projdoc_last_addendum'];
			}else{
				$projdoc_contract_no = $_POST['projdoc_kontrak_induk'];
			}
			
			$data['table'] = array_merge( $data['table'],
			array(
				'projdoc_swift_no'=>$_POST['projdoc_swift_no'],
				'projdoc_contract_no'=>$projdoc_contract_no,
			));
		}
		$created_by = $_SESSION[SESSION_USERID];
			
			
		$filter_criteria='m_document_document_id = ' . $document_id. ' and criteria_value < ' . $project_value;
		$criteria_info=$this->m_t_senddata->doctrac_getworkflowid($filter_criteria,'criteria_value DESC');
		$workflow_id=$criteria_info;
//--------------------- projdoc ------------------------------
		$data['table']['m_workflow_workflow_id']=$workflow_id;
		$data['table']['projdoc_created_date']=date('Y-m-d h:i:s');
		$data['table']['projdoc_created_by']=$created_by;
						
		//exe
		$projdoc_id = $this->m_t_senddata->create_projdoc($data['table']);
        
		//---------------------t_docflow-----------------------------
	   /*  $projdoc_last_rescord=$this->m_t_projdoc->last_record();
		$projdoc_id=$projdoc_last_rescord->projdoc_id; */
		
		// $document_id=$document_id;
		
		
		$filter = 'm_workflow_workflow_id = ' . $workflow_id;
		$sort = 'detworkflow_seq asc';
		$detworkflow_row = $this->m_t_senddata->doctrac_getdetworkflow($filter,$sort);
		
		$data['table'] = array(
				't_projdoc_projdoc_id'                  => $projdoc_id,
				't_projdoc_t_projectlist_projectlist_id'=>$projectlist_id,
				'docflow_recstat'                       =>'i',
				'docflow_seq'                           => 1,					       	
				'docflow_sentstat'                      =>'s',
				'docflow_sentdate'                      =>date('Y-m-d h:i:s'),
				'docflow_annotation'                    => $job_name,
				'docflow_stat'                          =>$detworkflow_row->detworkflow_stat,
				'm_detworkflow_detworkflow_id'          =>$detworkflow_row->detworkflow_id,
				'm_detworkflow_detworkflow_seq'         =>$detworkflow_row->detworkflow_seq,
			   
				);
		$data['table']['docflow_created_date']=date('Y-m-d h:i:s');
		$data['table']['docflow_created_by']=$created_by;
		$t_docflowid = $this->m_t_senddata->create_tdocflow($data['table']);
		
		
		//-------------------t_checklistflow------------------------
		$docflow_last_record=$this->m_t_senddata->tdocflow_lastrecord();
		$docflow_id=$docflow_last_record->docflow_id;
		$docflow_seq=$docflow_last_record->docflow_seq;
		$filter_checklist=' m_workflow_workflow_id = ' . $workflow_id . ' and m_detworkflow_detworkflow_seq = 1 ' . ' and checklist_active = "y"';                                
		
		$checklist= $this->m_t_senddata->read_vchecklist($filter_checklist);
		$error = 0;
		foreach($checklist as $row){
			
			$data['table'] = array('t_docflow_docflow_id'=>$docflow_id,
									't_docflow_docflow_seq'=>$docflow_seq,
									'checklist_id'=>$row->checklist_id,
									'checklist_accstat'=>'s'
								);
			$data['table']['checklist_created_date']=date('Y-m-d h:i:s');
			$data['table']['checklist_created_by']=$created_by;
			$result = $this->m_t_senddata->create_tchecklistflow($data['table']);
			if($result == 0){
				$error++;
			}
		}
		$this->db->trans_complete();
		if($error == 0){
			$this->m_t_senddata->update_status($kontrak_id,$jenis_data);
		}
		echo $error;
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
		
}
?>